<?php
namespace app\web\controller;

use app\model\Theme; 
use app\model\Share; 
use app\model\Scan as Scan_mod;

class Scan extends Base
{
    public function Scan()
    {
global $_G, $_P;
$system = $_G['system'];
$_G['page']['title'] = '参团列表 - ' . $system['sysname'];
//load_func('tpl');
$GLOBALS['frames'] = array();
$themeid = intval($_P['themeid']);
if($themeid <= 0){
	$themeid = Theme::getDefaultThemenew();
}
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
	return_msg('还没有创建活动,请联系管理员');
}
$config = 	Theme::getConfig($themeid);
$id = intval($_P['id']);
$do = 'fans';
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$url = createWebUrl($do,array('op' => 'display','themeid'=>$themeid));
if($op == 'delete') {
	db_delete(t_scan, array('id' => $id,'themeid'=>$themeid));
	return_msg('删除成功！', $url, 'success');
}


if($op == 'status') {
	$status = intval($_P['status']);
	$state = db_update(t_scan, array('status' => $status), array( 'id' => $id));
	return_msg(error(0, ''), '', 'ajax');
}

if($op == 'display'){
	
	$statustr = array("同城同行","百分百同行","此链接投诉过","此服务器投诉过","黑名单、可能同行","正常","检查员","手工白名单");
	$statustr2 = array("系统锁定","系统锁定","系统锁定","系统锁定","黑名单","正常","","手工白名单");
	$themelist = Theme::getAllTheme();
	$pageNumber = max ( 1, intval ( $_P['page'] ) );
    if ($theme['endtime']<strtotime("-90 day")) {
        if ($pageNumber>1 && $_G['username']!=='benben') {
            return_msg('活动结束超3个月,无法查看');
        }
        $pageSize = 20;
    }elseif ($theme['endtime']<strtotime("-30 day")) {
        if ($pageNumber>1 && $_G['username']!=='benben' && $_G['username']!=='caiwu' && $_G['username']!=='chuna') {
            return_msg('活动结束超1个月,无法查看');
        }
        $pageSize = 20;
    }else{
		$pageSize = 50;
	}
	
	if (empty($_P['createtime']['start'])){
		$starttime =db_fetchcolumn ( 'SELECT min(addtime) FROM ' . tablename ( t_fans ) );
		$starttime =  strtotime(date('Y-m-d 00:00:00',$starttime));
	}else{
		$starttime =  strtotime($_P['createtime']['start']);
	}
	$endtime = empty($_P['createtime']['end']) ? strtotime(date('Y-m-d 23:59:59',TIMESTAMP)): strtotime($_P['createtime']['end']) ;

	if(!isset($_P['type'])){
		$type = -1;
	}else{
		$type = intval($_P['type']);
	}

	$condition = " and 1=1 ";	
	$condition = " and ord.themeid = '{$themeid}' ";
	$condition.= " and ord.addtime > '{$starttime}' and ord.addtime <= '{$endtime}' ";

	
	$keyword = $_P['keyword'];
	if(!empty($keyword)){
		$condition.= " and (ord.fromuid = '{$keyword}' or ord.uid = '{$keyword}' )";
	}
	$fromuid = $_P['fromuid'];
	if(!empty($fromuid)){
		$keyword = $fromuid;
		$condition.= " and (ord.fromuid = '{$fromuid}' or ord.uid = '{$fromuid}' )";
	}

	$total = db_fetchcolumn ( 'SELECT COUNT(ord.id) FROM ' . tablename ( t_scan ) . " as ord
			WHERE  1=1  {$condition} ORDER BY ord.id desc",$params);

	
	$sql = "SELECT ord.* FROM " . tablename ( t_scan ) . " as ord
						WHERE  1=1  {$condition} 
						      ORDER BY ord.id asc LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;
	$list = db_select ( $sql,$params);
	foreach ($list as $index =>$row){
		$fansinfo = db_find ( "SELECT * FROM " . tablename ( t_fans ) . " WHERE uid = :uid and themeid =:themeid",array(":themeid"=>$themeid,":uid"=>$row['uid']));

		if($config['isicon'] > 0){
		        if($config['iconnum2'] <= 0){
		            $lightsum = Share::getNumByThemeIdAndOpenidAndStatus($themeid,$fansinfo['openid'],1);
		        }else{
		            $lightsum = Scan_mod::getNumByThemeIdAndOpenidAndStatus($themeid,$fansinfo['openid'],1);;
		            $lightsum = intval($lightsum/$config['iconnum2']);
		        }
				if($lightsum >= $config['iconnum']){
					$lightsum = $config['iconnum'];
				}
		        $list[$index]['lightsum'] =$lightsum;

		}
		if(!empty($fansinfo['workerid'])){
			$fans = db_find ( "SELECT w.username,t.name as teamname,s.name as companyname FROM " . tablename ( t_worker ) . " as w
				left join " . tablename ( t_team ) . " as t on w.team = t.id
				left join " . tablename ( t_shangjia ) . " as s on w.company = s.id
				WHERE w.uid = :uid and w.themeid =:themeid
				",array(":themeid"=>$themeid,":uid"=>$fansinfo['workerid']));
			$list[$index]['workername'] = $fans['username'];
			$list[$index]['teamname'] = $fans['teamname'];
			$list[$index]['companyname'] = $fans['companyname'];
		}
		$list[$index]['fansinfo'] = $fansinfo;

		if(!empty($row['fromuid'])){
			$fans = db_find ( "SELECT * FROM " . tablename ( t_fans ) . " WHERE uid = :uid and themeid =:themeid",array(":themeid"=>$themeid,":uid"=>$row['fromuid']));
			$list[$index]['nickname2'] = $fans['nickname'];
			$list[$index]['avatar2'] = $fans['avatar'];
		}
	}
	$pager = pagination ( $total, $pageNumber, $pageSize );
}

$return_clicent = get_defined_vars();
return view('/scan' , $return_clicent);
}
}